<template>
    <div>
        <div style="margin-left: 30px;margin-top: 15px;">
            <el-form :inline="true" :model="form" class="demo-form-inline">
                <div style="clear: both;">
                    <div style="float: left;">
                        <el-form-item label="后裔耳号">
                            <span>
                                <el-input v-model="form.input" style="width: 240px" />&nbsp;&nbsp;&nbsp;
                            </span>
                        </el-form-item>
                    </div>
                    <div style="float: right;">
                        <el-button type="danger" size="large" @click="showsheepfather">查询</el-button>
                        <el-button size="large" onclick="location.reload()">重置</el-button>
                    </div>
                </div>
            </el-form>
            <div style="margin-top: 20px;">
                <el-button type="danger" size="large" @click="OpenVisible" :icon="Plus">新增</el-button>
                <el-button type="danger" size="large" @click="DelAllInfo()" :icon="Delete">删除</el-button>
            </div>
            <div v-if="TagBool">
                <el-tag style="width:1920px;color: black;height: 50px;" type="danger" :iocn="Delete">
                    <spna style="font-size: 15px">
                        <Warning style="width: 1em; height: 1em; margin-right: 8px" color="red" />
                        <span style="margin-right: 1800px">已选择 {{ TagCount }} 项</span>
                    </spna>
                </el-tag>
            </div>
            <div v-else>
                <el-tag style="width:1920px;color: black;height: 50px;" type="success" :iocn="Delete">
                    <spna style="font-size: 15px">
                        <SuccessFilled style="width: 1em; height: 1em; margin-right: 8px" color="green" />
                        <span style="margin-right: 1800px">已选择 {{ TagCount }} 项</span>
                    </spna>
                </el-tag>
            </div> <br />
            <div style="margin-top: 35px;">
                <el-table :data="sheepList" style="width: 100%" @selection-change="handleSelectionChange" border>
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="序号" width="120" fixed>
                        <template v-slot="a">
                            {{ a.$index + (page.pageIndex - 1) * page.pageSize + 1 }}
                        </template>
                    </el-table-column>
                    <!-- <el-table-column prop="DocumentNumber" label="单据号" /> -->
                    <el-table-column prop="MaleDate" label="期初日期↓">
                        <template v-slot="scope">
                            <span>{{ scope.row.MaleDate ? scope.row.MaleDate.replace("T", " ").substring(0, 11) : null
                                }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="Earbugles" label="耳号" />
                    <el-table-column prop="CottageName" label="栋舍名称" />
                    <el-table-column prop="FieldName" label="栏位" />
                    <el-table-column prop="WeighMale" label="重量(公斤)" />
                    <el-table-column prop="CreateBy" label="创建人" />
                    <el-table-column prop="CreateDate" label="创建时间">
                        <template v-slot="scope">
                            <span>{{ scope.row.CreateDate ? scope.row.CreateDate.replace("T", " ").substring(0, 19) :
                                null
                                }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="UpdateBy" label="修改人" />
                    <el-table-column prop="UpdateDate" label="修改时间">
                        <template v-slot="scope">
                            <span>{{ scope.row.UpdateDate ? scope.row.UpdateDate.replace("T", " ").substring(0, 19) :
                                null
                                }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="ExamineBy" label="审核人" />
                    <el-table-column prop="ExamineDate" label="审核时间">
                        <template v-slot="scope">
                            <span>{{ scope.row.ExamineDate ? scope.row.ExamineDate.replace("T", " ").substring(0, 19) :
                                null
                                }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="ExamineType" label="审核状态">
                        <template v-slot="scope">
                            <div v-if="scope.row.ExamineType === false">
                                <span style="color: red;">未审核</span>
                            </div>
                            <div v-else>
                                <span style="color: green;">已审核</span>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="180">
                        <template v-slot="scope">
                            <div v-if="scope.row.ExamineType === false">
                                <el-button link type="primary" size="default"
                                    @click="handleClickExamine(scope.row.MaleId)">
                                    审核
                                </el-button>
                                <el-button link type="primary" size="default" @click="handleClickDel(scope.row.MaleId)">
                                    删除
                                </el-button>
                                <el-button link type="primary" size="default"
                                    @click="scope.row.isShow = true, UpdInfo(scope.row.MaleId)">修改</el-button>
                            </div>
                            <div v-else>
                                <el-button link type="primary" size="default"
                                    @click="handleClickExamineNew(scope.row.MaleId)">
                                    重新审核
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
                <table style="text-align: center;">
                    <tr>
                        <td>
                            <div>
                                <el-pagination v-model:current-page="page.pageIndex" v-model:page-size="page.pageSize"
                                    :page-sizes="[1, 15, 30, 45]" :background="true"
                                    layout="slot,total, sizes, prev, pager, next, jumper" :total="page.totalCount"
                                    @size-change="handleSizeChange" @current-change="handleCurrentChange" />
                            </div>
                        </td>
                    </tr>
                </table>
                <div style="margin: auto;text-align: right;margin-top: 20px;">
                    <span font-size="24pt">当前第{{ page.pageIndex }}页/共{{ page.pageCount }}页，共{{ page.totalCount
                        }}条数据</span>
                </div>
            </div>
            <el-dialog v-model="dialogTableVisible" title="后裔期初" width="80%" style="height: 60%;"
                :before-close="NewUpdate">
                <div>
                    <el-button type="danger" size="default" @click="addsheepInfo" :icon="Plus"
                        v-if="sheepInfo.MaleId == 0">保存</el-button>
                    <el-button type="danger" size="default" @click="addsheepInfo" :icon="DocumentChecked"
                        v-else>保存</el-button>
                </div>
                <br>
                <br>
                <span>后裔日期</span>&nbsp;&nbsp;&nbsp;&nbsp;<el-date-picker v-model="sheepInfo.MaleDate" type="date"
                    value-format="YYYY-MM-DD" />
                <br>
                <br>
                <el-table :data="rowList" style="width: 90%" border>
                    <el-table-column prop="Earbugles" label="耳号">
                        <span>
                            <el-input v-model="sheepInfo.Earbugles"></el-input>
                        </span>
                    </el-table-column>
                    <el-table-column prop="CottageName" label="栋舍">
                        <span>
                            <el-select v-model="sheepInfo.CottageName" placeholder="请选择" style="width: 180px"
                                @change="GetField()">
                                <el-option v-for="item in Cottage" :key="item.Id" :label="item.Name"
                                    :value="item.Name" />
                            </el-select>
                        </span>
                    </el-table-column>
                    <el-table-column prop="FieldName" label="栏位">
                        <span>
                            <el-select v-model="sheepInfo.FieldName" placeholder="请选择" style="width: 180px">
                                <el-option v-for="item in Field" :key="item.Id" :label="item.Name" :value="item.Name" />
                            </el-select>
                        </span>
                    </el-table-column>
                    <el-table-column prop="NumberMale" label="只数">
                        <span>
                            <el-input v-model="sheepInfo.NumberMale" disabled></el-input>
                        </span>
                    </el-table-column>
                    <el-table-column prop="WeighMale" label="重量（公斤）">
                        <span>
                            <el-input v-model="sheepInfo.WeighMale"></el-input>
                        </span>
                    </el-table-column>
                </el-table>
            </el-dialog>
        </div>
    </div>
</template>
<style scoped>
.el-pagination {
    justify-content: center;
}
</style>
<script setup lang="ts">
import http from "@/http"
import { ref, reactive, inject, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { AxiosStatic } from 'axios';
import {
    Plus,
    Search,
    Delete,
    DocumentChecked,
    Back,
    Warning,
    SuccessFilled
} from '@element-plus/icons-vue'
const axios = inject("axios") as AxiosStatic

//栋舍集合
const Cottage = ref([]);
//栋舍下拉方法
const GetCooter = () => {
    http.get("OrganizationRead/api/Cottage/HandleGetCot").then(res => {
        Cottage.value = res.data;
    })
}
//栏位集合
const Field = ref([]);
//栏位下拉方法
const GetField = () => {
    http.get("OrganizationRead/api/Field/HandleGetSheepFarm?Id=" + sheepInfo.CottageName).then(res => {
        Field.value = res.data;
    })
}

//添加弹出层初始值
const dialogTableVisible = ref(false);
//调用添加弹出层
const OpenVisible = () => {
    addFormInfo();
    sheepInfo.MaleDate = "";
    sheepInfo.MaleId = 0;
    dialogTableVisible.value = true;
}
//获取选中的数据
const sheepSelList = ref<{
    MaleId: any,
    DocumentNumber: any,
    Earbugles: any,
    CottageName: any,
    FieldName: any,
    NumberMale: any,
    WeighMale: any,
    MaleDate: any,
    CreateBy: any,
    CreateDate: any,
    UpdateBy: any,
    UpdateDate: any,
    ExamineBy: any,
    ExamineDate: any,
}[]>([])

const UpdateId = ref(0);

//选中数据
let ids = "";
const TagBool = ref(true);
const TagCount = ref(0);
const handleSelectionChange = (val: sheepList | any) => {
    sheepSelList.value = val;
    ids = "";
    val.forEach(x => {
        ids += x.MaleId + ",";
    })
    ids = ids.substring(0, ids.length - 1);
    TagCount.value = ids.split(',').length;
    if (ids == "") {
        TagCount.value = 0;
    }
    if (TagCount.value == 0) {
        TagBool.value = true;
    }
    else {
        TagBool.value = false;
    }
}
//点击修改事件弹出层
const UpdInfo = (MaleId: number | any) => {
    UpdateId.value = MaleId;
    addFormInfo();
    if (MaleId != 0) {
        http.get("routechen/api/ReadSheep/HandleGetSheep?maleId=" + MaleId).then(res => {

            sheepInfo.MaleId = res.data.MaleId;
            sheepInfo.CottageName = res.data.CottageName;
            sheepInfo.Earbugles = res.data.Earbugles;
            sheepInfo.FieldName = res.data.FieldName;
            sheepInfo.NumberMale = res.data.NumberMale;
            sheepInfo.WeighMale = res.data.WeighMale;
            sheepInfo.MaleDate = res.data.MaleDate;
            sheepInfo.SheepType = res.data.SheepType;
            sheepInfo.UpdateBy = res.data.UpdateBy;
            sheepInfo.UpdateDate = res.data.UpdateDate;
            sheepInfo.CreateBy = res.data.CreateBy;
            sheepInfo.CreateDate = res.data.CreateDate;
            sheepInfo.ExamineBy = res.data.ExamineBy;
            sheepInfo.ExamineDate = res.data.ExamineDate;
            sheepInfo.ConvertDate = res.data.ConvertDate;
        })
        let sheepUpdInfo = sheepList.value.find(item => item.MaleId == MaleId);
        sheepInfo.MaleId = sheepUpdInfo?.MaleId;
        sheepInfo.Earbugles = sheepUpdInfo?.Earbugles;
        sheepInfo.CottageName = sheepUpdInfo?.CottageName;
        sheepInfo.FieldName = sheepUpdInfo?.FieldName;
        sheepInfo.NumberMale = sheepUpdInfo?.NumberMale;
        sheepInfo.WeighMale = sheepUpdInfo?.WeighMale;
        sheepInfo.MaleDate = sheepUpdInfo?.MaleDate;
        sheepInfo.SheepType = sheepUpdInfo?.SheepType;
        sheepInfo.UpdateBy = sheepUpdInfo?.UpdateBy;
        sheepInfo.UpdateDate = sheepUpdInfo?.UpdateDate;
        sheepInfo.CreateBy = sheepUpdInfo?.CreateBy;
        sheepInfo.CreateDate = sheepUpdInfo?.CreateDate;
        sheepInfo.ExamineBy = sheepUpdInfo?.ExamineBy;
        sheepInfo.ExamineDate = sheepUpdInfo?.ExamineDate;
        sheepInfo.ConvertDate = sheepUpdInfo?.ConvertDate;
        dialogTableVisible.value = true;
    }
    console.log(sheepInfo.value);

}
//重新审核
const handleClickExamineNew = (MaleId: number) => {
    let maleId = {
        MaleId: MaleId,
    };
    ElMessageBox.confirm(
        '确定要重新审核吗?',
        '警告',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    )
        .then(() => {
            http.put("webroute/api/Sheep/HandleFatherNewExamine", maleId).then(res => {
                if (res.data > 0) {
                    ElMessage({
                        type: "success",
                        message: "重新审核",
                        duration: 2000
                    });
                    showsheepfather();
                } else {
                    ElMessage({
                        type: "error",
                        message: "重新审核失败",
                        duration: 2000
                    })
                    showsheepfather();
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '审核取消',
            })
        })
}
//判断是否审核通过
const handleClickExamine = (MaleId: number) => {
    let maleId = {
        MaleId: MaleId,
    };
    ElMessageBox.confirm(
        '确定确认审核通过吗?',
        '警告',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    )
        .then(() => {
            http.put("webroute/api/Sheep/HandleFatherExamine", maleId).then(res => {
                if (res.data > 0) {
                    ElMessage({
                        type: "success",
                        message: "审核通过",
                        duration: 2000
                    });
                    showsheepfather();
                } else {
                    ElMessage({
                        type: "error",
                        message: "审核失败",
                        duration: 2000
                    })
                    showsheepfather();
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '审核取消',
            })
        })
}
//批量删除
const DelAllInfo = () => {
    ElMessageBox.confirm(
        '确定要删除当前数据吗?',
        '警告',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    ).then(() => {
        http.delete("webroute/api/Sheep/HandleSheepAllDel?maleId=" + sheepSelList.value.map((item) => item.MaleId).toString()).then(res => {
            if (res.data > 0) {
                ElMessage({
                    type: "success",
                    message: "删除成功",
                    duration: 2000
                });
                showsheepfather();
            }
            else {
                ElMessage({
                    type: "error",
                    message: "删除失败",
                    duration: 2000
                });
                showsheepfather();
            }
        })
    })
}
//删除种公数据
const handleClickDel = (MaleId: number) => {
    ElMessageBox.confirm(
        '确定要删除当前数据吗?',
        '警告',
        {
            confirmButtonText: '是',
            cancelButtonText: '否',
            type: 'warning',
        }
    )
        .then(() => {
            http.delete("/webroute/api/Sheep/HandleSheepDel?maleId=" + MaleId).then(res => {
                if (res.data > 0) {
                    ElMessage({
                        type: "success",
                        message: "删除成功",
                        duration: 2000
                    });
                    showsheepfather();
                }
                else {
                    ElMessage({
                        type: "error",
                        message: "删除失败",
                        duration: 2000
                    });
                    showsheepfather();
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '删除取消',
            })
        })
}
//分页组件
const page = reactive({
    pageIndex: 1,
    pageSize: 15,
    totalCount: 0,
    pageCount: 0
})
//查询条件
const form = reactive({
    input: "",
    sheepType: 3,
    sdate: "",
})
//新增种公表单
const sheepInfo = reactive({
    MaleId: 0,
    Earbugles: "",
    CottageName: "",
    FieldName: "",
    NumberMale: "",
    WeighMale: "",
    MaleDate: "",
    UpdateBy: "",
    UpdateDate: "",
    CreateBy: "",
    CreateDate: "",
    ExamineBy: "",
    ExamineDate: "",
    ConvertDate: "",
    SheepType: 3,
});
//新增行表单
var newRow = {
    Earbugles: "",
    CottageName: "",
    FieldName: "",
    NumberMale: 1,
    WeighMale: "",
    MaleDate: "",
    isShow: true
}
//新增行
const rowLength = ref(0);

const rowList = ref([]);
const addFormInfo = () => {
    sheepInfo.MaleDate = "";
    sheepInfo.Earbugles = "";
    sheepInfo.CottageName = "";
    sheepInfo.FieldName = "";
    sheepInfo.NumberMale = 1;
    sheepInfo.WeighMale = "";
    rowList.value.push(newRow);
    console.log(rowLength.value);
}

//每页显示条数
const handleSizeChange = (val: number) => {
    page.pageSize = val;
    showsheepfather();
}
//当前页索引
const handleCurrentChange = (val: number) => {
    page.pageIndex = val;
    showsheepfather();
}
//种公列表
const sheepList = ref([])
//种公列表方法
const showsheepfather = () => {
    http.get("routechen/api/ReadSheep/HandleShowSheep", {
        params: {
            pageIndex: page.pageIndex,
            pageSize: page.pageSize,
            sheepType: form.sheepType,
            FatherEarTag: form.input
        }
    }).then((res) => {
        sheepList.value = res.data.datas;
        rowLength.value = res.data.datas.length;
        page.pageCount = res.data.pageCount;
        page.totalCount = res.data.totalCount;
    })
}
const EarInfo = ref<{
    earNumber: any,
    useCondition: any
}[]>([])

const EarListInfo = () => {
    http.get("MYroute/api/LamBing/LamBingGets").then(res => {
        EarInfo.value = res.data;
        console.log(EarInfo.value.filter(x => x.useCondition == 2));
    })
}
//添加种公信息
const addsheepInfo = () => {
    if (sheepInfo.MaleId == 0) {
        if (EarInfo.value.find(x => x.earNumber == sheepInfo.Earbugles) == undefined) {
            ElMessage.error("该耳号不存在");
            return;
        }
        if (sheepInfo.MaleDate == "") {
            ElMessage.error("期初日期不能为空");
            return;
        }
        if (sheepInfo.Earbugles == "") {
            ElMessage.error("耳号不能为空");
            return;
        }
        if (sheepInfo.CottageName == "") {
            ElMessage.error("栋舍不能为空");
            return;
        }
        if (sheepInfo.FieldName == "") {
            ElMessage.error("栏位不能为空");
            return;
        }
        if (sheepInfo.WeighMale == "") {
            ElMessage.error("重量不能为空");
            return;
        }
        http.post("webroute/api/Sheep/HandleSheepAdd", sheepInfo).then(res => {
            if (res.data > 0) {
                http.put("Mroute/api/LamBing/LamBingUpdates?card=" + sheepInfo.Earbugles);
                ElMessage({
                    type: "success",
                    message: "保存成功",
                    duration: 2000
                })
                dialogTableVisible.value = false;
                showsheepfather();
            } else if (res.data == -1) {
                ElMessage({
                    type: "error",
                    message: "保存失败，耳号不能重复",
                    duration: 2000
                })
            } else {
                ElMessage({
                    type: "error",
                    message: "保存失败",
                    duration: 2000
                })
                showsheepfather();
            }
        })
    }
    else {
        http.put("webroute/api/Sheep/HandleSheepUpd", sheepInfo).then(res => {
            if (res.data > 0) {
                ElMessage({
                    type: "success",
                    message: "修改成功",
                    duration: 2000
                })
                NewUpdate();
                dialogTableVisible.value = false;
                showsheepfather();
            } else if (res.data == -1) {
                ElMessage({
                    type: "error",
                    message: "修改失败，该耳号已存在",
                    duration: 2000
                })
                NewUpdate();
                showsheepfather();
            } else {
                ElMessage({
                    type: "error",
                    message: "修改失败",
                    duration: 2000
                })
                NewUpdate();
                showsheepfather();
            }
        })
    }
}

const NewUpdate = () => {
    sheepList.value.forEach(item => {
        if (item.MaleId == UpdateId.value) {
            item.isShow = false;
            showsheepfather();
            rowList.value = [];
        }
    })
    rowList.value = [];
    showsheepfather();
    dialogTableVisible.value = false;
}

onMounted(() => {
    showsheepfather();
    GetCooter();
    EarListInfo();
})

</script>
